import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:f_demo/ui/pages/index.dart';
import 'package:f_demo/utils/navigator_animation.dart';

class SplashPage extends StatefulWidget {
  @override
  _SplashPageState createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage> {
  Timer _timer;
  int _count = 1;

  @override
  void initState() {
    waitingApp(context);
    super.initState();
  }

  waitingApp(context) {
    _timer = Timer.periodic(Duration(seconds: 1), (index) {
      if (_count <= 1) {
        // Navigator.of(context)
        //     .pushNamedAndRemoveUntil('/home', (Route<dynamic> route) => false);

        Navigator.pushAndRemoveUntil(context, FadeRoute(page: TabPage()),
            (Route<dynamic> route) => false);

        _timer.cancel();
      } else {
        setState(() {
          _count--;
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.instance =
        ScreenUtil(allowFontScaling: true, width: 750, height: 1334)
          ..init(context);

    return Scaffold(
      body: Container(
        width: ScreenUtil().setWidth(750),
        height: ScreenUtil().setHeight(1334),
        color: Colors.red,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            AnimatedSwitcher(
              transitionBuilder: (child, anim) {
                return ScaleTransition(child: child, scale: anim);
              },
              duration: Duration(milliseconds: 300),
              child: Text(
                '${_count}s',
                key: ValueKey(_count),
                style: TextStyle(fontSize: 50, color: Colors.white),
              ),
            )
          ],
        ),
      ),
    );
  }
}
